package com.lzyjkz.quick_admin.web.controller.user;

import com.lzyjkz.quick_admin.web.request.SysUserLoginRequestDTO;
import com.lzyjkz.quick_admin.web.request.SysUserRegisterRequestDTO;
import com.lzyjkz.quick_components.entity.Response;
import com.lzyjkz.quick_components.service.TokenService;
import com.lzyjkz.quick_components.utils.ValidatorUtils;
import com.lzyjkz.quick_system.entity.SysUser;
import com.lzyjkz.quick_system.security.SysUserDetails;
import com.lzyjkz.quick_system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/user/account")
public class UserAccountController {

    private final SysUserService sysUserService;
    private final AuthenticationManager authenticationManager;
    private final TokenService tokenService;
    @PostMapping("/register")
    public Response<String> register(@RequestBody SysUserRegisterRequestDTO user) throws Exception {
        ValidatorUtils.validateEntity(user);
        if (sysUserService.lambdaQuery().eq(SysUser::getUsername, user.getUsername()).exists()) {
            return Response.fail("用户名已存在");
        }

        if (!user.getPassword().equals(user.getConfirmedPassword())){
            return Response.fail("两次输入的密码不一致");
        }

        SysUser sysUser = new SysUser();
        sysUser.setUsername(user.getUsername());
        sysUser.setPassword(user.getPassword());

        String register = sysUserService.register(sysUser);
        if (register.equals("注册成功")) {
            log.info("用户注册成功：{}", user.getUsername());
            return Response.success(register);
        }
        return Response.fail(register);
    }

    @PostMapping("/login")
    public Response<String> login(@RequestBody SysUserLoginRequestDTO user) throws Exception {
        ValidatorUtils.validateEntity(user);
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());
        Authentication authentication = null;
        try {
            authentication = authenticationManager.authenticate(authenticationToken);
        } catch (Exception e) {
            return Response.fail("用户名或密码错误");
        }
        UserDetails principal = (SysUserDetails) authentication.getPrincipal();

        String userToken = tokenService.createUserToken(principal);
        return Response.success(userToken, "登录成功");
    }
}
